<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>装卸地址管理 - TMS</title>
    <link rel="stylesheet" href="/view/frame/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/view/frame/static/css/global.css?v=1.0">
    <link rel="stylesheet" href="/view/tms/static/css/iframe.css?v=1.0">
</head>
<style>
    .layui-form-item {
        margin: 0;
    }
</style>

<body>
    <!--start: 搜索-->
    <div class="panel-handle search-bar">
        <form class="layui-form layui-form-pane" autocomplete="off">
            <div class="layui-row layui-col-space5">
                <div class="layui-col-sm1" style="width:35%;">
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width:100px;">装卸单位简称</label>
                        <div class="layui-input-block" style="margin-left:100px;">
                            <input type="text" name="coNameShort" placeholder="" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm1" style="width:25%;">
                    <div class="layui-form-item">
                        <label class="layui-form-label">联系人</label>
                        <div class="layui-input-block">
                            <input type="text" name="uname" placeholder="" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm1" style="width:25%;">
                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            <select name="status">
                                <option value="" selected="selected">全部</option>
                                <option value="1">启用</option>
                                <option value="0">停用</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm1" style="width:15%;">
                    <div class="layui-form-item">
                        <button class="layui-btn layui-btn-danger" lay-submit lay-filter="search"><i class="layui-icon">&#xe615;</i>查询</button>
                        <button class="layui-btn layui-btn-primary" type="reset" style="margin-left:2px; line-height:36px;">重置</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <!--end: 搜索-->
    <div style="display:none"><a class="bbqq" data-type="handingSiteAdd" data-func="callback(obj)" ; href="javascript:;">增加</a></div>
    <!--start: 按钮组-->
    <div class="panel-handle btns-bar">
        <div class="layui-btn-group fl">
            <button class="layui-btn layui-btn-normal" data-type="addData">新增</button>
            <button class="layui-btn layui-btn-normal" data-type="editData">编辑</button>
            <button class="layui-btn layui-btn-normal" data-type="detailData">查看</button>
            <button class="layui-btn layui-btn-normal" data-type="statusCancle">停用</button>
            <button class="layui-btn layui-btn-normal" data-type="statusStart">启用</button>
        </div>
        <div class="clr"></div>
    </div>
    <!--end: 按钮组-->

    <!--start:表格列表-->
    <table class="layui-hide" id="tableList" lay-filter="tbar"></table>
    <!--end:表格列表-->

    <script type="text/html" id="statusTpl">
        {{# if(d.status == 1){ }}
        <a lay-event="statusCancle" style="color:#1E9FFF;">启用</a> {{# }else{ }}
        <a lay-event="statusStart" style="color:#FF5722;">停用</a> {{# } }}
    </script>
    <script type="text/html" id="idTpl">
        {{d.LAY_TABLE_INDEX+1}}
    </script>
</body>
<script charset="utf-8" src="/view/frame/static/js/jquery.min.js?v=1.0"></script>
<script charset="utf-8" src="/view/frame/layui/layui.js"></script>
<script charset="utf-8" src="/view/tms/static/js/tms_index.js?v=1219"></script>

<script charset="utf-8" src="/view/frame/static/js/errorCodeMap.js"></script>
<script charset="utf-8" src="/view/frame/static/js/HC.js?v=1.4"></script>
<script charset="utf-8" src="/view/tms/static/js/validator.js"></script>
<script charset="utf-8" src="/view/tms/static/js/bizUtil.js"></script>

<script>
    layui.use(['form', 'layer', 'table', 'tms_tab', 'laydate'], function() {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            tmsTab = layui.tms_tab,
            laydate = layui.laydate,
            $ = layui.jquery;
        //初始化日期时间
        laydate.render({
            elem: '#doarkDateStart'
        });
        laydate.render({
            elem: '#doarkDateEnd'
        });
        //初始化表格   
        setTableData();
        var $id = $.trim(getUrlParam('id'));
        // console.log($id);
        //监听提交
        form.on('submit(search)', function(data) {
            var $coNameShort = $.trim(data.field.coNameShort),
                $uname = $.trim(data.field.uname),
                $status = $.trim(data.field.status);

            var $parms = 'coNameShort=' + $coNameShort + '&uname=' + $uname + '&status=' + $status;
            setTableData($parms);
            return false;
        });

        function callback(obj) {
            if (typeof obj.data === 'object') {
                obj = obj.data;
            }
            return obj;
        }
        //方法级渲染
        function setTableData(parms) {
            parms = typeof parms != 'undefined' ? parms : '';
            var $pageSize = 15;

            table.render({
                elem: '#tableList',
                url: '/ucenter/crm/customers/customersAddress/search.shtml?custId=0&' + parms,
                // where: $parms,
                response: {
                    statusCode: 'SUCCESS',
                    countName: 'objects.total', //数据总数的字段名称，默认：count
                    dataName: 'objects'
                },
                request: {
                    pageName: 'pageNum', //页码的参数名称，默认：page
                    limitName: 'pageSize' //每页数据量的参数名，默认：limit
                },
                cols: [
                    [{
                        checkbox: true,
                        fixed: true
                    }, {
                        title: '序号',
                        width: 80,
                        align: 'center',
                        templet: '#idTpl'
                    }, {
                        field: 'coNameShort',
                        title: '装卸单位简称',
                        width: 150
                    }, {
                        field: 'address',
                        title: '地址',
                        width: 180
                    }, {
                        field: 'uname',
                        title: '联系人',
                        width: 100,
                        align: 'center'
                    }, {
                        field: 'mobile',
                        title: '联系方式',
                        width: 130,
                        align: 'center'
                    }, {
                        field: 'tel',
                        title: '电话',
                        width: 150,
                        align: 'center'
                    }, {
                        field: 'status',
                        title: '状态',
                        width: 80,
                        templet: '#statusTpl',
                        align: 'center'
                    }, {
                        field: 'notes',
                        title: '注意事项',
                        width: 200
                    }]
                ],
                id: 'dataReload',
                page: false,
                limits: [15, 30, 50, 100],
                limit: $pageSize,
                height: 'full-130',
                even: true
            });
        }

        //监听每一行的事件处理
        table.on('tool(tbar)', function(obj) {
            var data = obj.data;
            console.log(JSON.stringify(data));
            if (obj.event === 'detail') {
                //事件
            }
        });

        var active = {
            //新增
            addData: function() {
                window.handAdd = function() {
                    var cllBack = $('.bbqq').attr('data-func');
                    var Fun = eval(cllBack);
                    var objs = Fun;
                    var reb = false;
                    if (objs.conAdd == 1) {
                        reb = true;
                    }
                    delete objs.conAdd;
                    $.ajax({
                        type: 'post',
                        url: '/ucenter/crm/customers/customersAddress/add.shtml?id=0',
                        dataType: "json",
                        contentType: "application/json",
                        data: JSON.stringify(objs),
                        success: function(d) {
                            var $code = d.code,
                                $msg = d.msg,
                                $objects = d.objects;

                            if ($code === 'SUCCESS') {
                                parent.layer.closeAll();
                                var $layId = $(window.parent.document).find('.layui-this').attr('lay-id');
                                parent['f' + $layId].location.reload();
                                if (reb) {
                                    active.handingSiteAdd();
                                }
                            }
                        }
                    });
                }

                parent.layer.open({
                    type: 2,
                    title: '新增装卸单位',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['900px', '600px'],
                    content: './handingSiteTpl.html?do=add',
                    success: function(layero, index) {},
                    btn: ['确定', '关闭'],
                    yes: function(index, layero) {
                        $(layero.find('iframe')[0].contentWindow.document).find('#vsubmit').click();
                    }
                });
            },
            //编辑
            editData: function() { //获取选中数据
                var checkStatus = table.checkStatus('dataReload'),
                    data = checkStatus.data[0];

                window.handEdit = function() {
                    var cllBack = $('.bbqq').attr('data-func')
                    console.log(cllBack);
                    var Fun = eval(cllBack);
                    var objs = Fun;
                    // Fun;
                    delete objs.$id;
                    delete objs.conAdd;
                    objs['id'] = data.id;
                    // console.log($id);
                    //保存数据，调用接口
                    $.ajax({
                        type: 'PUT',
                        url: '/ucenter/crm/customers/customersAddress/0.shtml',
                        dataType: "json",
                        contentType: "application/json",
                        data: JSON.stringify(objs),
                        success: function(d) {
                            console.log(d);
                            var $code = d.code,
                                $msg = d.msg,
                                $objects = d.objects;

                            if ($code === 'SUCCESS') {
                                // data.update(objs);
                                parent.layer.closeAll();
                                parent.layer.alert('保存成功！', {
                                    yes: function() {
                                        //关闭窗口
                                        parent.layer.closeAll();

                                        //获取当前框架ID并刷新

                                        var $layId = $(window.parent.document).find('.layui-this').attr('lay-id');
                                        parent['f' + $layId].location.reload();
                                    }
                                });
                            } else {
                                parent.layer.alert('保存信息失败，请重新填写！');
                            }
                        }
                    });
                }
                if (data.length == 0) {
                    parent.layer.alert('你还未选中数据！');
                } else if (data.length > 1) {
                    parent.layer.alert('只能选择一条数据！');
                } else {
                    // tmsTab.add($(this), '编辑价格协议（' + data[0].targetName + '）', 'modify.html?do=edit&id=' + data[0].id+'&targetType='+data[0].targetType);
                    parent.layer.open({
                        type: 2,
                        title: '编辑装卸单位',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['900px', '600px'],
                        content: './handingSiteTpl.html?do=edit&idD=' + data.id,
                        success: function(layero, index) {},
                        btn: ['确定', '关闭'],
                        yes: function(index, layero) {
                            $(layero.find('iframe')[0].contentWindow.document).find('#vsubmit').click();
                        }
                    });
                }
            },
            //查看
            detailData: function() {
                var checkStatus = table.checkStatus('dataReload'),
                    data = checkStatus.data[0];

                if (data.length == 0) {
                    parent.layer.alert('你还未选中数据！');
                } else if (data.length > 1) {
                    parent.layer.alert('只能选择一条数据！');
                } else {
                    parent.layer.open({
                        type: 2,
                        title: '查看装卸单位',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['900px', '500px'],
                        content: './handingSiteTpl.html?do=detail&idD=' + data.id + '&addressId=' + data.addressAreaId
                    });
                }
            },
            //停用
            statusCancle: function() {
                var checkStatus = table.checkStatus('dataReload'),
                    data = checkStatus.data;

                if (data.length == 0) {
                    parent.layer.alert('你还未选中数据！');
                } else {
                    parent.layer.confirm('您确定要停用供应商吗？', function(index) {
                        var $ids = [];
                        for (var $i = 0; $i < data.length; $i++) {
                            if (data[$i].status == 3) {
                                parent.layer.alert(data[$i].name + '已经是停用状态！');
                                return;
                            }
                            $ids.push(data[$i].id);
                        }
                        roleStatus($ids, 3);
                        parent.layer.close(index);
                    });
                }
            },
            //启用
            statusStart: function() {
                var checkStatus = table.checkStatus('dataReload'),
                    data = checkStatus.data;
                if (data.length == 0) {
                    parent.layer.alert('你还未选中数据！');
                } else {
                    parent.layer.confirm('您确定要启用供应商吗？', function(index) {
                        var $ids = [];
                        for (var $i = 0; $i < data.length; $i++) {
                            if (data[$i].status == 1) {
                                parent.layer.alert(data[$i].name + '已经是启用状态！');
                                return;
                            }
                            $ids.push(data[$i].id);
                        }
                        roleStatus($ids, 1);
                        parent.layer.close(index);
                    });
                }
            }
        };

        $('.btns-bar .layui-btn').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //改变状态，1启用，2禁用
        function roleStatus($ids, $status) {
            var $saveData = {
                ids: $ids,
                status: $status
            }

            $.ajax({
                type: 'post',
                url: '/ucenter/crm/customers/customersAddress/use.shtml',
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify($saveData),
                success: function(d) {
                    var $code = d.code,
                        $msg = d.msg,
                        $objects = d.objects;

                    if ($code === 'SUCCESS') {
                        window.location.reload();
                    } else {
                        parent.layer.alert('操作失败！');
                    }
                }
            });
        }
    });
</script>

</html>